import datetime
import pymysql
from login1 import funclogin


def func1(yuju, neirong, str1, str2):
    con = pymysql.connect(host='101.200.51.214', port=3306, user='root', password='123456',
                          database='messages', charset='utf8', autocommit=True)
    try:
        with con.cursor(cursor=pymysql.cursors.DictCursor) as cursor:
            result = cursor.execute(yuju, neirong)

            if result:
                print(str1)
            else:
                print(str2)
    except:
        print('输入内容错误请重新输入')


    finally:
        con.close()


while True:

    print(f'1.登录\t 2.注册\t 3.退出')
    choose1 = int(input('请选择：'))
    if choose1 == 1:
        # 登录
        name, password = input('请输入姓名：'), input('请输入密码：')
        yuju = 'select usename from tb_use where usename=%s and usepassword=%s'
        neirong, str1, str2 = (name, password), '登陆成功', '登陆失败'
        func1(yuju, neirong, str1, str2)
        funclogin(name)

    elif choose1 == 2:
        while True:
            # 注册
            name, password = input('请输入姓名：'), input('请输入密码：')
            now_time = datetime.datetime.now()
            yuju = 'insert into tb_use values (%s,%s,%s)'
            neirong, str1, str2 = (name, password, now_time), '注册成功', '注册失败'
            func1(yuju, neirong, str1, str2)
            print(f'1.继续注册\t 2.退出注册')
            choose2 = int(input('请输入选择:'))
            if choose2 == 2:
                break

    elif choose1 == 3:
        break